package ir.mirrajabi.searchdialog;

import ohos.agp.components.AttrHelper;
import ohos.agp.text.RichTextBuilder;
import ohos.agp.text.TextForm;

import java.util.Locale;

/**
 * Created by MADNESS on 5/16/2017.
 */
public class StringsHelper {
    /**
     * 给字体穿插上色
     *
     * @param str1           列表中某一项字符串
     * @param str2           搜索的字符
     * @param defaultColor   默认的颜色
     * @param highlightColor 红色
     * @param textSize       字体大小
     * @return 封装好的颜色字体
     */
    public static RichTextBuilder highLightLCS(
            String str1, String str2, int defaultColor, int highlightColor, int textSize) {
        String lcs = lcs(str1.toLowerCase(Locale.ROOT), str2.toLowerCase(Locale.ROOT));
        String tLower = str1.toLowerCase(Locale.ROOT);

        RichTextBuilder builder = new RichTextBuilder();

        int defaultFlag = 0;
        int flag = -1;

        for (int i = 0; i < tLower.length() && lcs.length() > 0; i++) {
            if (tLower.charAt(i) == lcs.charAt(0)) {
                lcs = lcs.substring(1);
                flag = i;

                if (flag - defaultFlag != 0) {
                    String strStart = str1.substring(defaultFlag, flag);
                    builder.mergeForm(new TextForm().setTextSize(textSize).setTextColor(defaultColor));
                    builder.addText(strStart);
                    builder.revertForm();
                }

                String strLast = str1.substring(flag, flag + 1);
                builder.mergeForm(new TextForm().setTextSize(textSize).setTextColor(highlightColor));
                builder.addText(strLast);
                builder.revertForm();

                flag = flag + 1;
                defaultFlag = flag;
            }
        }
        if (flag != -1 && defaultFlag != 0 && defaultFlag != str1.length()) {
            String strEnd = str1.substring(defaultFlag, str1.length());
            builder.mergeForm(new TextForm().setTextSize(textSize).setTextColor(defaultColor));
            builder.addText(strEnd);
            builder.revertForm();
        }

        return builder;
    }

    /**
     * 将搜索的字体与列表的某一项进行校对
     *
     * @param text1 某一项字符
     * @param text2 搜索字符
     * @return 校对字符串
     */
    public static String lcs(String text1, String text2) {
        int[][] lengths = new int[text1.length() + 1][text2.length() + 1];
        for (int i = 0; i < text1.length(); i++) {
            for (int j = 0; j < text2.length(); j++) {
                if (text1.charAt(i) == text2.charAt(j)) {
                    lengths[i + 1][j + 1] = lengths[i][j] + 1;
                } else {
                    lengths[i + 1][j + 1] = Math.max(lengths[i + 1][j], lengths[i][j + 1]);
                }
            }
        }
        StringBuffer sb = new StringBuffer();
        for (int x = text1.length(), y = text2.length(); x != 0 && y != 0; ) {
            if (lengths[x][y] == lengths[x - 1][y]) {
                x--;
            } else if (lengths[x][y] == lengths[x][y - 1]) {
                y--;
            } else {
                assert text1.charAt(x - 1) == text2.charAt(y - 1);
                sb.append(text1.charAt(x - 1));
                x--;
                y--;
            }
        }
        return sb.reverse().toString();
    }
}
